package com.uzai.console.service.sys.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.DesensitizeUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sys.shortlink.ShortLinkDto;
import com.uzai.console.entity.*;
import com.uzai.console.entity.redis.ShortLinkRedis;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.sys.ShortLinkService;
import com.uzai.console.vo.sys.shortlink.ShortLinkVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 短链接设置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class ShortLinkServiceImpl implements ShortLinkService {

    Logger logger = LoggerFactory.getLogger(ShortLinkServiceImpl.class);

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询短链接设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public ShortLinkVo findShortLinkInfo(Long merId){

        ShortLinkVo shortLinkVo = new ShortLinkVo();

        //查询运营商短链接设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.SHORT_LINK_CONFIG.getValue());
        if(merBaseConfig != null ){
            shortLinkVo =  JSONObject.parseObject(merBaseConfig.getVal(), ShortLinkVo.class);
        }

        //脱敏显示
        shortLinkVo.setTlbAppKey(DesensitizeUtil.around(shortLinkVo.getTlbAppKey(), 4, 4));
        shortLinkVo.setLlySecret(DesensitizeUtil.around(shortLinkVo.getLlySecret(), 4, 4));

        return shortLinkVo;
    }

    /**
     * 修改短链接设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param shortLinkDto
     * @return null
     */
    public void updateShortLink(ShortLinkDto shortLinkDto){

//        if(StringUtils.isBlank(shortLinkDto.getTlbAppId())){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入推老板appId");
//        }
//
//        if(StringUtils.isBlank(shortLinkDto.getTlbAppKey())){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入推老板appKey");
//        }


        //微信红包旧账房appId
        String tlbAppKey_old = "";
        //微信红包旧账房appKey
        String llySecret_old = "";

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(shortLinkDto.getMerId(), MerConfigKeyEnum.SHORT_LINK_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            ShortLinkRedis shortLinkRedis_old = JSONObject.parseObject(merBaseConfig.getVal(), ShortLinkRedis.class);
            if(shortLinkRedis_old != null){
                tlbAppKey_old = Tools.getStr(shortLinkRedis_old.getTlbAppKey());
                llySecret_old = Tools.getStr(shortLinkRedis_old.getLlySecret());
            }

            //如果包含星星，则不修改
            String tlbAppKey_new = Tools.getStr(shortLinkDto.getTlbAppKey());
            if(tlbAppKey_new.contains("*")){
                shortLinkDto.setTlbAppKey(tlbAppKey_old);
            }

            //如果包含星星，则不修改
            String llySecret_new = Tools.getStr(shortLinkDto.getLlySecret());
            if(llySecret_new.contains("*")){
                shortLinkDto.setLlySecret(llySecret_old);
            }

            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(shortLinkDto)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(shortLinkDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.SHORT_LINK_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(shortLinkDto)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        ShortLinkRedis shortLinkRedis = new ShortLinkRedis();
        BeanUtils.copyProperties(shortLinkDto, shortLinkRedis);
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.SHORT_LINK_CONFIG.getKey(), shortLinkDto.getMerId()), Tools.getStrEmpty(JSONObject.toJSONString(shortLinkRedis)));
    }

}
